package com.sywb.chuangyebao.view;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.UserInfo;
import com.sywb.chuangyebao.contract.BindingAlipayContract;
import com.sywb.chuangyebao.utils.RegexUtils;
import com.sywb.chuangyebao.view.dialog.AlertDialog;
import com.sywb.chuangyebao.view.dialog.ConfirmAlertDialog;

import org.bining.footstone.db.DbManager;
import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;
import org.bining.footstone.utils.NetUtils;
import org.bining.footstone.utils.SharedUtils;
import org.bining.footstone.utils.ValidUtils;

import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 绑定支付宝
 */
public class AlipayBindingActivity extends ActionbarActivity<BindingAlipayContract.Presenter> implements BindingAlipayContract.View{
    @BindView(R.id.et_phone_number)
    EditText etPhoneNumber;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.tv_send_code)
    TextView tvSendCode;
    @BindView(R.id.et_alipay_account)
    EditText etAlipayAccount;
    @BindView(R.id.et_account_name)
    EditText etAccountName;
    @BindView(R.id.et_check_code)
    EditText etCheckCode;
    @BindView(R.id.tv_sing_hint)
    TextView tvSingHint;
    @BindView(R.id.introduce_tv)
    TextView introduceTv;
    private LinkedHashMap<Integer, String> errorMap = new LinkedHashMap<>();
    @Override
    public void initView(Bundle bundle) {
        super.initView(bundle);
        setTitle(R.string.binding_alipay);
        String openId = SharedUtils.getString(Constants.USEROPENID);
        UserInfo userInfo = DbManager.getInstance().queryById(openId, UserInfo.class);
        etPhoneNumber.setText(userInfo.mobile);
       /* TextWatcher mobileListener = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null) {
                    String mobile = editable.toString();
                    if (!TextUtils.isEmpty(mobile)) {
                        errorMap.remove(1);
                        if (mobile.length() == 11 && RegexUtils.checkMobile(mobile)) {
                            errorMap.remove(2);
                            if (NetUtils.isConnected()) {
                                errorMap.remove(10);
                                errorMap.put(3, "正在验证您的手机号码");
                                mPresenter.ssoExists(mobile);
                            } else {
                                errorMap.put(10, "网络异常,请检查网络");
                            }
                        } else {
                            errorMap.put(2, "请填写正确的手机号码");
                        }
                    } else {
                        errorMap.put(1, "请输入您的手机号码");
                    }
                    changeClickable();
                }
            }
        };
        etPhoneNumber.addTextChangedListener(mobileListener);*/

        TextWatcher captchaListener = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null) {
                    String captcha = s.toString();
                    if (!TextUtils.isEmpty(captcha)) {
                        errorMap.remove(4);
                        if (captcha.length() == 4 && ValidUtils.isNumber(captcha)) {
                            errorMap.remove(5);
                        } else {
                            errorMap.put(5, "请输入您收到的手机验证码");
                        }
                    } else {
                        errorMap.put(4, "请输入您的验证码");
                    }
                    changeClickable();
                }
            }
        };
        etCheckCode.addTextChangedListener(captchaListener);

        TextWatcher alipayListener = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null) {
                    String captcha = s.toString();
                    if (!TextUtils.isEmpty(captcha)) {
                        errorMap.remove(6);
                        if (captcha.length() > 0 ) {
                            errorMap.remove(7);
                        } else {
                            errorMap.put(7, "请输入支付宝账号");
                        }
                    } else {
                        errorMap.put(6, "请输入支付宝账号");
                    }
                    changeClickable();
                }
            }
        };
        etAlipayAccount.addTextChangedListener(alipayListener);

        TextWatcher nameListener = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null) {
                    String captcha = s.toString();
                    if (!TextUtils.isEmpty(captcha)) {
                        errorMap.remove(8);
                        if (captcha.length() >=2) {
                            errorMap.remove(9);
                        } else {
                            errorMap.put(9, "请输入你的姓名");
                        }
                    } else {
                        errorMap.put(8, "请输入你的姓名");
                    }
                    changeClickable();
                }
            }
        };
        etAccountName.addTextChangedListener(nameListener);
        errorMap.put(8, "请输入您的姓名");
        errorMap.put(6, "请输入您的支付宝账号");
        errorMap.put(4, "请输入您的验证码");
        //errorMap.put(1, "请输入您的手机号码");
        btnConfirm.setClickable(false);
        btnConfirm.setBackgroundResource(R.drawable.round20_pie_gray);
        setVerificationState(null, true);
    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
    }

    public void changeClickable() {
        if (errorMap.isEmpty()) {
            btnConfirm.setClickable(true);
            btnConfirm.setBackgroundResource(R.drawable.round20_pie_theme);
            tvSingHint.setVisibility(View.GONE);
            introduceTv.setVisibility(View.VISIBLE);
        } else {
            btnConfirm.setClickable(false);
            btnConfirm.setBackgroundResource(R.drawable.round20_pie_gray);
            for (Map.Entry<Integer, String> entry : errorMap.entrySet()) {
                tvSingHint.setText(entry.getValue());
            }
            tvSingHint.setVisibility(View.VISIBLE);
            introduceTv.setVisibility(View.GONE);
        }
    }
    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.activity_alipay_binding;
    }

    @Override
    public void resultIsRegister(boolean isRegister, String message) {
        errorMap.remove(3);
        if (NetUtils.isConnected()) {
            errorMap.remove(10);
           /* if (!isRegister) {
                errorMap.remove(11);
                setVerificationState(null, true);
            } else {
                errorMap.put(11, "该手机号已注册,请直接登录");
            }*/

        } else {
            errorMap.put(10, "网络异常,请检查网络");
        }
        changeClickable();
    }

    @Override
    public void setVerificationState(String text, boolean isClick) {
        if (TextUtils.isEmpty(text)) {
            tvSendCode.setText(R.string.code_get);
        } else {
            tvSendCode.setText(text);
        }
        tvSendCode.setClickable(isClick);
        tvSendCode.setTextColor(isClick ? ContextCompat.getColor(mActivity, R.color.colorBlue) : ContextCompat.getColor(mActivity, R.color.colorDark));
    }
    @OnClick({R.id.tv_send_code})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tv_send_code:
                mPresenter.sendCode(etPhoneNumber.getText().toString());
            break;
        }
    }

    @Override
    public EditText getCode() {
        return etCheckCode;
    }

    @Override
    public EditText getAlipayAccount() {
        return etAlipayAccount;
    }

    @Override
    public EditText getAlipayName() {
        return etAccountName;
    }

    @Override
    public boolean useRxBus() {
        return true;
    }
    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.POST_BINDING_ALIPAY)})
    public void bindingAlipayResult(String s) {
        ConfirmAlertDialog alertDialog = ConfirmAlertDialog.newInstance("您已成功绑定支付宝账号",null);
        alertDialog.show(getMyFragmentManager(), "AlipayBindingActivity.showConfirmDialog");
        alertDialog.setClickListener(new AlertDialog.OnClickListener() {
            @Override
            public void onClick(int position) {
                if (position==0){
                    finish();
                    advance(AlipayWithdrawActivity.class);
                }
            }
        });
    }

    public Button getBtnConfirm() {
        return btnConfirm;
    }
}
